<template>
  <div class="login-box">
    <div class="login-inner">
      <h3 class="title">平台管理系统</h3>
      <el-form
        :model="loginFrom"
        :rules="rules"
        ref="loginFrom"
        class="demo-loginFrom"
      >
        <!-- 用户名 -->
        <el-form-item prop="uname">
          <el-input
            type="text"
            v-model="loginFrom.uname"
            autocomplete="off"
            prefix-icon="iconfont icon-zhanghao"
          ></el-input>
        </el-form-item>
        <!-- 密码 -->
        <el-form-item prop="pass">
          <el-input
            :type="iseyes ? 'text' : 'password'"
            v-model="loginFrom.pass"
            autocomplete="off"
            prefix-icon="iconfont icon-mima"
            :suffix-icon="iseyes ? 'iconfont icon-eyes-' : 'iconfont icon-eyes'"
            @click.native="showPass"
            @keydown.native.enter="loginsubmit('loginFrom')"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            class="loginSubmit"
            @click="loginsubmit('loginFrom')"
            :loading="submit"
            >提交</el-button
          >
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import { login } from '@/api/user.js'
import local from '@/utils/local.js'
export default {
  data() {
    return {
      loginFrom: { uname: '', pass: '' },
      rules: {
        uname: [{ required: true, message: '请输入账号', trigger: 'blur' }],
        pass: [{ required: true, message: '请输入密码', trigger: 'blur' }]
      },
      // 控制眼镜开闭
      iseyes: false,
      submit: false
    }
  },
  methods: {
    showPass(e) {
      if (e.target.className.includes('icon-eyes')) {
        this.iseyes = !this.iseyes
      }
    },
    loginsubmit(ref) {
      // 防止用户频繁触发
      if (this.submit) return
      this.submit = true
      // 触发表单的验证
      this.$refs[ref].validate(async valid => {
        // 成功触发
        if (valid) {
          // 发送请求
          const { code, msg, token } = await login({
            account: this.loginFrom.uname,
            password: this.loginFrom.pass
          })
          // 判断是否登录成功
          if (code === 0) {
            // 登录成功
            this.$message({
              type: 'success',
              message: msg
            })
            local.set('token', token)
            setTimeout(() => {
              this.$router.push('/')
            }, 1500)
          } else {
            // 登录失败
            this.submit = false
            this.$message.error(msg)
          }
        } else {
          // 失败触发
          this.submit = false
          this.$message.error('请输入正确账号')
        }
      })
    }
  }
}
</script>

<style lang="less" scoped>
.login-box {
  height: 100%;
  background-color: #2d3a4b;
  display: flex;
  align-items: center;
  justify-content: center;
  .login-inner {
    width: 400px;
    .title {
      text-align: center;
      font-size: 20px;
      padding: 30px 0;
      font-weight: 700;
      color: #fff;
      letter-spacing: 2px;
    }
    /deep/.el-input__suffix .iconfont {
      cursor: pointer;
    }
    /deep/.el-input__inner {
      background-color: #283443;
    }
    .loginSubmit {
      width: 100%;
    }
  }
}
</style>
